﻿(async () => {
    await window.ris.initialize();

    $(() => {
        const $eventEmitter = $('body > .container');
        let reloadGrid = null;
        let exportParams = null;
        let exportData = (func) => {
            if (func instanceof Function) {
                func(exportParams);
            }
        };
        $('.main').splitter({
            orientation: 'v',
            panel1Size: '300px',
        });
        const quickFilter = $('.quick-filter').quickFilter(
            `/api/filter/get-filter-by-type/queuing`,
            filterId => {
                reloadGrid = () => {
                    dataGrid.reload({ filterId });
                };
                reloadGrid();
            },
            {
                type: 'GET',
            }
        );

        const advancedSearch = $('.advanced-search').advancedSearchWithCollapse(null, (e) => {
            reloadGrid = () => {
                dataGrid.refresh({ userData: e });
            };
            reloadGrid();
        }, { id: 'technician' });
        const gridOperations = [];
        const gridContextMenuItems = [];
        const dataGrid = $('.exam-grid').examGrid({
            id: 'technician-exam-grid',
            key: 'oid',
            getOperations() {
                return gridOperations;
            },
            getContextMenuItems() {
                return gridContextMenuItems;
            },
            onLoad(loadOptions) {
                return new Promise((resolve, reject) => {
                    if (loadOptions.filterId || (loadOptions.userData && !$.isEmptyObject(loadOptions.userData))) {
                        exportParams = loadOptions;
                        $.post({
                            dataType: 'json',
                            contentType: 'application/json;charset=UTF-8',
                            url: `/api/technician/get-list`,
                            data: JSON.stringify(loadOptions),
                            success: result => {
                                if (result.success) {
                                    resolve(result)
                                } else {
                                    reject(result.error);
                                }
                            }
                        });
                    } else {
                        resolve({ success: true, data: { columns: [], rows: [], totalCount: 0 } })
                    }
                })
            },
            onRowDblClick(oid) {
                //工作流
                $.invokeAction('查看申请单',
                    `/api/technician/view-apply/${oid}`,
                    null,
                    {
                        type: 'GET',
                        actionUrl: '/api/workflow/invoke'
                    });
            },
            onRowPrepared(e) {
                if (e.rowType === 'data' && e.data) {
                    if (e.data.isEmergency) {
                        e.rowElement.css('background-color', '#c0392b16');
                    }
                }
            },
            onCellPrepared(e) {
                if (e.data) {
                    if (e.column.dataField === 'accessionNumber') {
                        e.cellElement.css('position', 'relative').append(createPatientTypeSign(e.data.outPatientNumber, e.data.inPatientNumber, e.data.physicalExamNumber));
                    } else if (e.column.dataField === 'reportStatus') {
                        e.cellElement.empty().append(createReportStatusBadge(e.data.reportStatus, e.data.isArchive));
                    } else if (e.column.dataField === 'queueStatus') {
                        e.cellElement.empty().append(createQueuingStatusBadge(e.data.queueStatus));
                    } else if (e.column.dataField === 'isEmergency') {
                        e.cellElement.empty().append(createEmergencyBadge(e.data.isEmergency));
                    }
                }
            },
        });

        const tools = $.getTechnicianTools($eventEmitter, quickFilter, advancedSearch, dataGrid, {
            reloadGrid: () => {
                if (reloadGrid instanceof Function) {
                    reloadGrid();
                }
            },
            exportData: (func) => {
                if (exportData instanceof Function) {
                    exportData(func);
                }
            }
        });

        const toolbar = $('.toolbar').dxToolbar({}).dxToolbar('instance');
        const toolbar1 = $('.toolbar1').dxToolbar({}).dxToolbar('instance');
        //判断是否已经启用了自动刷新
        tools.autoRefresh.options.text = autoRefresh('refreshInterval', 'interval', () => {
            if (reloadGrid instanceof Function) {
                reloadGrid();
            }
        }, true);
        tools.refreshSetting.options.items = [tools.autoRefresh.options, tools.refreshInterval.options]
        toolbar.option('items', [tools.viewApply, tools.openExam, tools.changeDevice, tools.viewReport, tools.viewImage,
        tools.callNext, tools.call, tools.emergencyRegist, tools.matchStudy, tools.others, tools.refresh, tools.refreshSetting]);
        toolbar1.option('items', [tools.start, tools.startExam, tools.completed, tools.recheck, tools.cancel, tools.suspend, tools.pass, tools.checkParameterRecord]);

        gridOperations.push(tools.viewApply, tools.openExam, tools.call, tools.start, tools.startExam, tools.completed, tools.applyConsultation);
        gridContextMenuItems.push(tools.viewApply, tools.openExam, tools.changeDevice, tools.viewReport, tools.viewImage,
            tools.examEvent, tools.export, tools.call, tools.start, tools.startExam, tools.completed,
            tools.recheck, tools.cancel, tools.suspend, tools.pass, tools.applyConsultation, tools.checkParameterRecord);
        $.get('/api/technician/get-advanced-search-view').then(result => {
            if (result.success) {
                if (result.view) {
                    advancedSearch.updateView(result.view, result.data);
                }
            }
        });
    });
})();
